/**
 * 详情对话框
 */
var WasteInfoInfoDlg = {
    data: {
        name:""
        ,wasteTypeId:""
        ,icon:""
        ,price:""
        ,status:""
        ,unitName:""
    }
};

layui.use(['form', 'ax','laydate','admin','upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;
    // 让当前iframe弹层高度适应
    admin.iframeAuto();

    var iconUploader = upload.render({
        elem:"#iconUpload",
        url:Feng.ctxPath+'/wasteInfo/editItem',
        auto:false,
        data:{
            id:function(){return $("#id").val()}
            ,name:function(){return $("#name").val()}
            ,wasteTypeId:function(){return $("#wasteTypeId").val()}
            ,icon:function(){return $("#icon").val()}
            ,price:function(){return $("#price").val()}
            ,status:function(){return $("#status").val()}
            ,unitName:function(){return $("#unitName").val()}
        },
        //bindAction:'#saveBtn',
        done:function(res){
            if(res.code==200){
                Feng.success("上传并保存成功！");
                admin.putTempData('formOk', true);

                //关掉对话框
                admin.closeThisDialog();
            }else{
                Feng.error("上传失败！");
            }

        },
        error:function(){

        }
    })

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/wasteInfo/detail/" + Feng.getUrlParam("id"));
    var result = ajax.start();
    form.val('wasteInfoForm', result.data);


    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var validation = new Validation($("#wasteInfoForm")[0], {onSubmit:false});
        var validate = validation.validate();
        if(validate){
            var ajax = new $ax(Feng.ctxPath + "/wasteInfo/editItem", function (data) {
                Feng.success("更新成功！");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);

                //关掉对话框
                admin.closeThisDialog();
            }, function (data) {
                Feng.error("更新失败！" + data.responseJSON.message)
            });
            ajax.set(data.field);
            var chooseFiles = $(".layui-upload-choose");
            if(chooseFiles.length>0){
                iconUploader.upload();
            }else{
                ajax.start();
            }

        }else{
            var serializeArray = $(".validation-failed");
            var errMsgs = "";
            for(var i=0;i<serializeArray.length;i++){
                var elemInfo = serializeArray[i];
                var elem = $("#"+elemInfo.name)[0];
                if(elem && elem.tooltip){
                    errMsgs+="<div><span style='color:dodgerblue'>"+elem.placeholder+"输入框有误：</span><span style='color:red'>"+elem.tooltip.content+"</span></div>"
                    var tipElem = elem.tagName=="SELECT"?$(elem).next():elem;
                    layer.tips(elem.tooltip.content,tipElem,{
                        tips: [1, '#FF99CC'],
                        time: 4000,
                        tipsMore: true
                    });
                }
            }
            //Feng.error(errMsgs);
        }
        return false;

    });

    //返回按钮
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + "/wasteInfo";
    });
    var laydate = layui.laydate;
    lay('.datetime-input').each(function(){
        laydate.render({
            elem: this
            ,trigger: 'click'
            ,type: 'datetime'
        });
    });
});